import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../views/user/login'
import Login from 'view/user/login'
import HomeLayout from "@/layouts/HomeLayout"
import NotFound from "view/404"
import users from "view/users"
import roles from "view/roles"
import rights from "view/rights"
import goods from "view/goods"
import params from "view/params"

import Welcome from "view/welcome"
import store from "store"

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    name: 'HomeLayout',
    component: HomeLayout,
    // redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: Welcome,
      },
      // {
      //   path: '*',
      //   name: '404',
      //   component: NotFound,
      // },
      {
        path: '/users',
        name: 'users',
        component: users,
      },
      {
        path: '/roles',
        name: 'roles',
        component: roles,
      },
      {
        path: '/rights',
        name: 'rights',
        component: rights,
      },
      {
        path: '/goods',
        name: 'goods',
        component: goods,
      },
      {
        path: '/params',
        name: 'params',
        component: params,
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  const token = store.getters.token
  const isLogin = to.name === 'login'
  // 如果有token，并且是在登录页跳转到首页
  if (token && isLogin) {
    return next({ name: 'HomeLayout' })
  }

  // 没有token，并且不是在登录页，跳转到登录页
  if (!token && !isLogin) {
    return next({ name: 'login' })
  }
  next()
})
export default router
